// DriveNotes. Copyright (c) 2011, Andrei Senchuk. All rights reserved.
package net.taviscaron.drivenotes.web.converter;

import java.util.Map;
import net.taviscaron.drivenotes.shared.model.entity.User;
import static net.taviscaron.drivenotes.web.util.ParamUtils.parseStringParam;

/**
 * New user converter
 * @author Andrei Senchuk
 */
public class NewUserConverter extends FormConverter<User> {
    @Override
    public User convert(Map params, String prefix) throws InvalidFormException {
        try {
            User user = new User();
            user.setName(parseStringParam(params, "name", prefix));
            user.setSurname(parseStringParam(params, "surname", prefix));
            user.setUsername(parseStringParam(params, "username", prefix));
            user.setEmail(parseStringParam(params, "email", prefix));
            String password = parseStringParam(params, "password", prefix);
            String password2 = parseStringParam(params, "password2", prefix);
            user.setPassword((password != null && password.equals(password2)) ? password : null);
            return user;
        } catch (IllegalArgumentException e) {
            throw new InvalidFormException();
        }
    }
}
